//
//  CompetitionAPi.m
//  CoolRollerSkating
//
//  Created by WSL on 15/11/23.
//  Copyright © 2015年 Coollang. All rights reserved.
//

#import "CompetitionAPi.h"
#import "RankModel.h"
#import "MyRankModel.h"

static NSString * const kCompetitionPath = @"/TennisSportController/getRanking";

@interface CompetitionAPi ()
@property (copy,nonatomic) NSString *type;
@property (copy, nonatomic) NSString *timePeriod;

@end

@implementation CompetitionAPi

- (instancetype)initWithRankType:(NSString *)type
                      timePeriod:(NSString *)timePeriod
{
    if ([super init]) {
        _type = type;
        _timePeriod = timePeriod;
    }
    return self;
}


-(id)requestArgument {
    return @{
             @"Type": self.type,
             @"Class": self.timePeriod
             };
}

- (NSString *)requestUrl{
    return kCompetitionPath;
}

- (YTKRequestMethod)requestMethod {
    return YTKRequestMethodPOST;
}

//- (id)jsonValidator{
//    
//    return @{@"ret":[NSString class],
//             @"errDesc":@{@"MyRankInfo":@{@"MaxSpeed":[NSString class],
//                                          @"Duration":[NSString class],
//                                          @"Number":[NSString class],
//                                          @"UserName":[NSString class],
//                                          @"Icon":[NSString class],
//                                          @"TotalCount":[NSString class]},
//                          @"RankList":[NSArray class]}};
//}

- (id)responseJSONObject{
    if(self.responseString==nil) return nil;
    return [NSJSONSerialization JSONObjectWithData:[self.responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
}

- (NSDictionary *)responseInfo{
    return [[self responseJSONObject] objectForKey:@"errDesc"];
}

#pragma mar - praseData

- (NSArray<RankModel *> *)rankItems {
    NSMutableArray *mutaArray = @[].mutableCopy;
    NSArray *array = [self.responseInfo objectForKey:@"RankList"];
    for (NSDictionary *dic in array) {
        RankModel *model = [[RankModel alloc] initWithDictionary:dic];
        [mutaArray addObject:model];
    }
    
    return mutaArray.copy;
}

- (MyRankModel *)myRankItem{
    NSDictionary *dic  = [self.responseInfo objectForKey:@"MyRankInfo"];
    MyRankModel *model = [[MyRankModel alloc] initWithDictionary:dic];
    return model;
}

@end
